/**
 * Created by 011926 on 2014/9/17.
 */

/**
 * form表单控件
 * verify方法 开始验证表单数据，并返回是否成功true/false
 * submitForm,对表单进行提交，url为form的action属性
 * @param obj
 */
var zhForm= function(obj)
{
    var formObj = $(obj);//form表单对象

    /**
     * 提交表格
     * 需要提供表格成功后的回调函数func
     */
    this.submitForm = function(func){
        if(formObj != null)
        {
            var formData= formObj.serializeArray();
            var url = formObj.attr("action");
            if(url==null || url =="")
            {
                alert("找不到提交路径，请设置form的action属性。");
                return;
            }
            $.ajax({
                type: "POST",
                url: url,
                data: formData,
                success: function(data){
                    func(data);
                }
            });
        }
    }

    /**
     * 开始验证表单数据，并在页面上有所展示
     * 验证通过返回true/失败返回false
     */
    this.verify = function()
    {
        if(formObj != null)
        {
            var isPass = true;//验证是否通过
            var allInputText = formObj.find("input[type='text']");//获得所有的input text
            var allCheckbox = formObj.find("input[type='checkbox']");//获得所有的input text
            var allRadio = formObj.find("input[type='radio']");//获得所有的input radio

            var allTextarea = formObj.find("textarea");//获得所有的Textarea
            var allSelect = formObj.find("select");//获得所有的select

            /**
             * 验证select，notNull 至少选择一个
             */
            allSelect.each(function(){
                var thisObj = $(this);
                //非空
                var notNull = thisObj.attr("notNull");
                if(notNull != null && notNull=="true")
                {
                    setSuccess(thisObj);
                    var value = thisObj.val();
                    if(value==null || value=="")
                    {
                        zAlert(thisObj,"请选择");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }
            });

            /**
             * 验证textarea，notNull不为空，minNum(最少字数)maxNum(最大字数)
             */
            $(allTextarea).each(function(){
                var thisObj = $(this);
                //非空
                var notNull = thisObj.attr("notNull");
                var maxNum = thisObj.attr("maxNum");
                var minNum = thisObj.attr("minNum");
                if(notNull == "true" || (maxNum!=null && maxNum > 0) || (minNum!=null && minNum > 0))//不为空
                {
                    setSuccess(thisObj);
                    var min = 0;
                    var max = Number.MAX_VALUE;
                    if(notNull == "true")
                    {
                        min = 1;
                    }
                    if(minNum!=null && minNum > 0)
                    {
                        min = minNum;
                    }
                    if(maxNum!=null && maxNum > 0)
                    {
                        max = maxNum;
                    }
                    var num =  thisObj.val().length;
                    if(num<min || num > max)
                    {
                        var msg = "";
                        if(min == max)
                        {
                            msg ="字数只能"+max+"个";
                        }
                        else if(max ==Number.MAX_VALUE)
                        {
                            msg ="至少写"+min+"个";
                        }
                        else{
                            msg ="字数在"+min+"-"+max+"之间";
                        }
                        zAlert(thisObj,msg);
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }
            });

            /**
             * 验证radio，notNull 至少选择一个
             */
            allRadio.each(function(){
                var thisObj = $(this);
                var name = thisObj.attr("name");
                //非空
                var notNull = thisObj.attr("notNull");
                if(notNull != null && notNull=="true")
                {
                    setSuccess(thisObj);
                    var num =  formObj.find("input[type='radio'][name='"+name+"']:checked").length;
                    if(num <= 0)
                    {
                        zAlert(thisObj,"请选择");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }

            });

            /**
             * 验证checkbox的最多(maxNum)、最少(minNum)选择个数；不为空notNull(至少选一个)
             */
            $(allCheckbox).each(function(){
                var thisObj = $(this);
                var name = thisObj.attr("name");
                //非空
                var notNull = thisObj.attr("notNull");
                var maxNum = thisObj.attr("maxNum");
                var minNum = thisObj.attr("minNum");
                if(notNull == "true" || (maxNum!=null && maxNum > 0) || (minNum!=null && minNum > 0))//不为空
                {
                    setSuccess(thisObj);
                    var min = 0;
                    var max = Number.MAX_VALUE;
                    if(notNull == "true")
                    {
                        min = 1;
                    }
                    if(minNum!=null && minNum > 0)
                    {
                        min = minNum;
                    }
                    if(maxNum!=null && maxNum > 0)
                    {
                        max = maxNum;
                    }
                    var num =  formObj.find("input[type='checkbox'][name='"+name+"']:checked").length;
                    if(num<min || num > max)
                    {
                        var msg = "";
                        if(min == max)
                        {
                            msg ="选择"+max+"个";
                        }
                        else if(max ==Number.MAX_VALUE)
                        {
                            msg ="至少选"+min+"个";
                        }
                        else{
                            msg ="选"+min+"-"+max+"个";
                        }
                        zAlert(thisObj,msg);
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }
            });

            /**
             * 主要验证非空notNull，数字验证isNumber，邮箱验证isEmail，手机电话验证isPhone，身份证isIDCard
             */
            $(allInputText).each(function(i){
                var thisObj = $(this);
                var value = thisObj.val();

                //非空
                var notNull = thisObj.attr("notNull");
                if(notNull == "true")//不为空
                {
                    setSuccess(thisObj);
                    if(value == "")
                    {
                        zAlert(thisObj,"不为空");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }

                //数字验证
                var isNumber = thisObj.attr("isNumber");
                if(isNumber == "true")//不为空
                {
                    setSuccess(thisObj);
                    if(value == null|| value == "" || isNaN(value))
                    {
                        zAlert(thisObj,"请输入数字");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }

                //邮箱验证
                var isEmail = thisObj.attr("isEmail");
                if(isEmail == "true")//不为空
                {
                    setSuccess(thisObj);
                    var emailV = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                    if(!emailV.test(value))
                    {
                        zAlert(thisObj,"格式错误");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }

                //手机、电话验证
                var isPhone = thisObj.attr("isPhone");
                if(isPhone == "true")//不为空
                {
                    setSuccess(thisObj);
                    var mobile = /^1[3|5|8]\d{9}$/ , phone = /^0\d{2,3}-?\d{7,8}$/;
                    if(mobile.test(value) || phone.test(value))
                    {
                        zAlert(thisObj,"格式错误");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }

                //身份证
                var isIDCard = thisObj.attr("isIDCard");
                if(isIDCard == "true")//不为空
                {
                    setSuccess(thisObj);
                    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                    if(reg.test(value))
                    {
                        zAlert(thisObj,"身份证错误");
                        setError(thisObj);
                        isPass = false;
                        return;
                    }
                }
            });
            return isPass;
        }
    }

    var setSuccess = function(obj)
    {
        if(obj != null)
        {
            $(obj).parent().removeClass("has-error");
            $(obj).parent().removeClass("has-success");
            $(obj).parent().addClass("has-success");
        }
    }

    var setError = function(obj)
    {
        if(obj != null)
        {
            $(obj).parent().removeClass("has-error");
            $(obj).parent().removeClass("has-success");
            $(obj).parent().addClass("has-error");
        }
    }
}

